// 用户接口
import { login } from '@/API/user'
// 提示
import { Message } from 'element-ui'
// 路由
// import router from '@/router'
export default {
  namespaced: true,
  state: {
    token: null, // token
    regionId: null, // 区域id
    repair: null, // 是否是维修员
    userId: null, // 用户id
    userName: null // 用户名称
  },
  getters: {},
  mutations: {
    setUserState (state, payload) {
      state.token = payload.token
      state.regionId = payload.regionId
      state.repair = payload.repair
      state.userId = payload.userId
      state.userName = payload.userName
    }
  },
  actions: {
    // 登录逻辑
    async loginAction ({ commit }, payload) {
      const res = await login(payload)
      // 判断是否登录成功
      if (res.success) {
        Message.success(res.msg)
        commit('setUserState', res)
      } else {
        Message.error(res.msg)
        return Promise.reject(new Error(res.msg))
      }
    },
    // 退出逻辑
    logout ({ commit }) {
      commit('setUserState', {})
    }
  }
}
